//
//  ProfileViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-6-6.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SettingViewController.h"
#import <MMDrawerController/MMDrawerBarButtonItem.h>
#import <UIViewController+MMDrawerController.h>
#import "BPush.h"
#import "WebViewController.h"
#import "UIAlertView+Kirinrin.h"
#import "EaseMob.h"

@interface SettingViewController ()<UIAlertViewDelegate>

@end

@implementation SettingViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}
*/

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        UIAlertView *confirmView = [[UIAlertView alloc]initWithTitle:@"确认" message:@"是否注销当前用户" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [confirmView show];

        
    }
    
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
                [self performSegueWithIdentifier:@"toWebView" sender:@"http://gabrielecirulli.github.io/2048/"];
                break;
            case 1:
                [self performSegueWithIdentifier:@"toWebView" sender:@"http://ibaby365.net/about.html"];
                break;
            case 2:
                [self performSegueWithIdentifier:@"toWebView" sender:@"http://ibaby365.net/weixin.html"];
                break;
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSString *)tableView:(UITableView *)table titleForFooterInSection:(NSInteger)section{
    NSLog(@"get footer text");
    switch (section) {
        case 0:
            return nil;
            break;
            
        case 1:{
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            
            // app版本
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            
            // app build版本
            NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
            
#ifdef DEBUG
            NSString *footerString = [NSString stringWithFormat:@"Version: %@\t Debug Build: %@", app_Version, app_build];
#else
            NSString *footerString = [NSString stringWithFormat:@"Version: %@\t Build: %@", app_Version, app_build];
#endif
            return footerString;
        }
            
            break;
    }
    return nil;
}


#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults] ;
        [defaults setBool:NO forKey:@"canAutoSignIn"];
        [defaults synchronize];
        
        
        NSArray *tags =@[self.appDelegate.userId, self.appDelegate.user[@"kClassId"]];
        [BPush delTags:tags];
        
        [[EaseMob sharedInstance].chatManager asyncLogoffWithCompletion:^(NSDictionary *info, EMError *error) {
            if (error) {
                NSLog(@"IM 退出失败");
                [UIAlertView alertWithTitle:@"错误" message:@"IM组件退出失败"];
            }
                //TODO: 清理IM退出的打开新的VC
                
                
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                
                UIViewController *signInController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"SignIn"];
                
                self.view.window.rootViewController = signInController;

            
        } onQueue:nil];
            }
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toWebView"]) {
        WebViewController *webViewController = (WebViewController *)segue.destinationViewController;
        webViewController.urlString = sender;
        
    }
}


@end
